﻿package mortal.game.view.task.drama.operations
{
    import Message.DB.Tables.*;
    import com.gengine.debug.*;
    import flash.utils.*;
    import mortal.game.manager.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.task.data.*;

    public class TaskDramaNpcTalk extends Object implements ITaskDramaStepCommand
    {
        private var _callback:Function;
        private var timerId:int = -1;
        private var _data:TTaskDrama;

        public function TaskDramaNpcTalk()
        {
            return;
        }// end function

        public function call(param1:TTaskDrama, param2:Function = null) : void
        {
            this._data = param1;
            if (this.timerId > 0)
            {
                Log.error("TaskDramaNpcTalk, timerId > 0");
            }
            this._callback = param2;
            var _loc_3:* = ThingUtil.npcUtil.getNpc(param1.talkNpc);
            var _loc_4:* = param1.talkText.length / 10 * 1000 + param1.conditionEntity * 1000;
            if (_loc_3 == null)
            {
                _loc_3 = RolePlayer.instance;
            }
            if (_loc_3 != null)
            {
                LayerManager.entityTalkLayer.addTalk(_loc_3, GLinkTextDataParser.parseToHtmlFormat(param1.talkText), _loc_4);
                this.timerId = setTimeout(this.talkFinished, _loc_4);
            }
            return;
        }// end function

        private function talkFinished() : void
        {
            this.timerId = -1;
            if (this._data != null)
            {
                this.cancel(this._data, this._callback);
            }
            return;
        }// end function

        public function cancel(param1:TTaskDrama, param2:Function = null) : void
        {
            this._callback = param2;
            var _loc_3:* = ThingUtil.npcUtil.getNpc(param1.talkNpc);
            if (_loc_3 == null)
            {
                _loc_3 = RolePlayer.instance;
            }
            if (_loc_3 != null)
            {
                LayerManager.entityTalkLayer.removeTalk(_loc_3);
            }
            if (this.timerId > 0)
            {
                clearTimeout(this.timerId);
                this.timerId = -1;
            }
            if (this._callback != null)
            {
                this._callback.apply();
                this._callback = null;
            }
            return;
        }// end function

        public function dispose() : void
        {
            if (this.timerId > 0)
            {
                clearTimeout(this.timerId);
                this.timerId = -1;
            }
            return;
        }// end function

    }
}
